// subPages/health/healthAdvice/healthAdvice.js
const app = getApp();
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type='experts'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btn:{
      btnTxt: '跳转外部专家咨询',
      src: '/resources/health/right2.png',
      imgW: '26'
    },
    currentSwiper: 0,
    consultNavList: json.consultNavList,
    expertsClassList:[{class_name: '全部专家'}],
    expertsClassCurrent: 0,
    classId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: (res) => {
        //创建节点选择器
        that.setData({
          windowWidth: res.windowWidth
        })
      },
    })
    loadMoreView = that.selectComponent("#loadMoreView")
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      })
      //获取健康咨询banner
      util.getAjax(app.globalData.apiUrl +'/get_community_health_expert_banner',that.getBanner,{community_id: that.data.communityId})
      //获取门店电话
      util.getAjax(app.globalData.apiUrl +'/admin_operation_community_address_phone',that.getCommunityPhone,{id: that.data.communityId,type: 3})
      //获取专家分类
      util.getAjax(app.globalData.apiUrl +'/get_community_health_expert_class',that.getExpertsClass,{community_id: that.data.communityId})
      that.getExpertsAllData(that.data.classId)
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          })
          //获取健康咨询banner
          util.getAjax(app.globalData.apiUrl +'/get_community_health_expert_banner',that.getBanner,{community_id: that.data.communityId})
          //获取门店电话
          util.getAjax(app.globalData.apiUrl +'/admin_operation_community_address_phone',that.getCommunityPhone,{id: that.data.communityId,type: 3})
          //获取专家分类
          util.getAjax(app.globalData.apiUrl +'/get_community_health_expert_class',that.getExpertsClass,{community_id: that.data.communityId})
          that.getExpertsAllData(that.data.classId)
        }
      }
    }
    
  },

  //获取banner
  getBanner(res){
    var that = this
    // console.log(res)
    if(res.data.length != 0){
      var bannerImgList = []
      for(var i in res.data){
        bannerImgList.push(res.data[i])
      }
      that.setData({
        isBanner: true,
        bannerImgList: bannerImgList,
        currentSwiper: 0
      })
    }
  },

  /***  门店电话  ***/
  getCommunityPhone(res){
    var that = this
    // console.log(res)
    if(res.data.address_phone){
      that.setData({
        address_phone: res.data.address_phone
      })
    }
  },

  //获取专家分类
  getExpertsClass(res){
    // console.log(res)
    var that = this
    if(res.data.length != 0){
      var navList = []
      for(var i in res.data){
        navList.push(res.data[i])
      }
      let list = that.data.expertsClassList.concat(navList)
      that.setData({
        expertsClassList: list
      })
    }
  },

  //查看未读数量
  getUnreadNumber(res){
    // console.log(res)
    var that = this
    that.setData({
      isMessage: res.data?true:false,
      num: res.data
    })
  },

  //获取专家数据
  getExpertsAllData:function(classId){
    //获取数据数据
    util.getAjax(app.globalData.apiUrl +'/get_community_health_expert_all',this.getExpertsData,{p: p,class_id: classId,num: num,community_id: this.data.communityId},this.shopDataErr)
  },

  getExpertsData(res){
    var that = this
    // console.log(res)
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.expertsAllList.concat(res.data.list)
      }
      that.setData({
        expertsAllList: list
      })
    }else{
      that.setData({
        expertsAllList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取专家数据异常
  expertDataErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取专家失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getExpertsAllData(this.data.classId)
  },

  clickLoadMore: function(e) {
    this.getExpertsAllData(this.data.classId)
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;
    if(that.data.expertsClassList.length > 3){
      //每个tab选项宽度占1/4
      var singleNavWidth = that.data.windowWidth / 4;
      //tab选项居中                            
      that.setData({
        navScrollLeft: (cur - 1.5) * singleNavWidth
      })
    }else{
      //每个tab选项宽度占1/3
      var singleNavWidth = that.data.windowWidth / 3;
      //tab选项居中                            
      that.setData({
        navScrollLeft: (cur - 1) * singleNavWidth
      })
    }                          
    if (that.data.expertsClassCurrent == cur) {
      return false;
    } else {
      that.setData({
        expertsClassCurrent: cur
      })
      // console.log(that.data.expertsClassList[that.data.expertsClassCurrent])
      that.setData({
        classId: that.data.expertsClassCurrent != 0?that.data.expertsClassList[that.data.expertsClassCurrent].id:''
      })
      p = 1
      that.getExpertsAllData(this.data.classId)
    }
  },

  swiperChange:function(e){
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  //咨询按钮
  expertsBtn:function(e){
    var that = this
    if(that.data.userId){
      let item = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/subPages/health/consult/consult?type=' + 1 + '&item=' + JSON.stringify(item),
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //拨打电话
  phoneClick:function(e){
    var that = this
    if(that.data.address_phone){
      wx.makePhoneCall({
        phoneNumber: that.data.address_phone
      })
    }else{
      util.showModal('提示','门店尚未添加咨询电话')
    }
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      //用户查看未读数量
      util.getAjax(app.globalData.apiUrl +'/user_get_communtiy_health_expert_tall',that.getUnreadNumber,{user_id: that.data.userId})
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          })
          //用户查看未读数量
          util.getAjax(app.globalData.apiUrl +'/user_get_communtiy_health_expert_tall',that.getUnreadNumber,{user_id: that.data.userId})
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getExpertsAllData(this.data.classId)
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})